import api from '../../api/api'
import {couponList, InsertCouponUsers} from "../../api/conf";

Page({
    data:{
        couponType: "0",
        couponList: [],
        page: 1,
        size: 6,
        couponListLoadStatus: 0,
        receiveBtnText: "立即领取"
    },
    onLoad: function(options) {
        // 页面初始化 options为页面跳转所带来的参数
        const { couponType } = options;
        this.setData({
            couponType,
        });
    },
    onReady: function() {
        // 页面渲染完成
        this.setData({
            couponList: [],
            page: 1,
            couponListLoadStatus: 0
        })
        this.getCouponList()
    },
    onReachBottom() {
        if (this.data.couponListLoadStatus !== 0) {
           return;
        }
        this.setData({
            page: this.data.page + 1
        })
        this.getCouponList()
    },
    async getCouponList(){
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        wx.showNavigationBarLoading()
        this.setData({ couponListLoadStatus: 1 });

        this.setReceiveBtnText()
        await api.get(couponList, {
            type: this.data.couponType,
            page: this.data.page,
            size: this.data.size
        }).then(data => {
            this.setData({
                couponList: this.data.couponList.concat(data.Data.Items),
                couponListLoadStatus: 0,
            })
            if (data.Data.Paging.TotalPage === this.data.page) {
                this.setData({
                    couponListLoadStatus: 2
                })
            }
            wx.hideLoading()
            wx.hideNavigationBarLoading()
        }).catch(err => {
            wx.showToast({
                title: err.message,
                icon: 'none'
            })
        })
    },
    setReceiveBtnText() {
        switch (this.data.couponType){
            case "0":
                this.setData({
                    receiveBtnText: "立即领取"
                })
                break;
            case "1":
                this.setData({
                    receiveBtnText: "立即使用"
                })
                break;
            case "2":
                this.setData({
                    receiveBtnText: "已使用"
                })
                break;
            case "3":
                this.setData({
                    receiveBtnText: "已过期"
                })
                break;
        }
    },

    couponTap(e) {
        this.setData({
            couponType: e.detail.value,
            couponList: [],
            page: 1,
            couponListLoadStatus: 0
        })
        this.getCouponList()
    },
    receiveCoupon(e) {
        let couponType = this.data.couponType
        if(couponType === "0"){
            wx.showLoading({
                title: '加载中',
                mask: true
            })
            api.get(InsertCouponUsers, {
                couponId: e.detail.couponid
            }).then( data => {
                wx.hideLoading()
                let toast = 'success'
                let title = '领取成功'
                if(!data.Data){
                    toast = "error"
                    title = "领取失败"
                }
                wx.showToast({
                    title: title,
                    icon: toast,
                    duration: 2000
                })
            })
        }else if(couponType === "1") {
            wx.navigateTo({
                url: "/pages/subcategory/subcategory",
            });
        }
    }
})